package cn.ghf.pet.service.impl;

import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.DistanceUtil;
import cn.ghf.basic.util.PageList;
import cn.ghf.basic.util.Point;
import cn.ghf.org.domain.Employee;
import cn.ghf.org.domain.Shop;
import cn.ghf.org.mapper.EmployeeMapper;
import cn.ghf.org.mapper.ShopMapper;
import cn.ghf.pet.domain.Pet;
import cn.ghf.pet.domain.PetDetail;
import cn.ghf.pet.domain.SearchMasterMsg;
import cn.ghf.pet.mapper.PetDetailMapper;
import cn.ghf.pet.mapper.PetMapper;
import cn.ghf.pet.mapper.SearchMasterMsgMapper;
import cn.ghf.pet.query.SearchMasterMsgQuery;
import cn.ghf.pet.service.ISearchMasterMsgService;
import cn.ghf.user.domain.User;
import cn.ghf.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    /**
     * 宠物寻主信息发布
     * @param searchMasterMsg
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg,Long loginInfoId) {
        //获取对应的userid  写一个工具类，取获取登录信息
        //通过请求头获取token，取redis中获取对应的值
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        //通过百度地图，添加地址
        //获取地址，把地址转成经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //获取所有shop
        List<Shop> shops = shopMapper.loadAll();

        //获取最近的用户
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //保存信息
        searchMasterMsgMapper.add(searchMasterMsg);
    }

    /**
     * 待处理消息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        query.setState(0);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        //否则就是平台管理员
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryData(query);
    }

    /**
     * 已处理消息
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        query.setState(1);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        //否则就是平台管理员
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryData(query);
    }

    /**
     * 宠物待处理消息处理功能
     * @param pet
     * @param id
     */
    @Transactional
    @Override
    public void handle(Pet pet, Long id) {
        //1.更新状态，state = 1
        searchMasterMsgMapper.updateStateBySmmId(pet.getSearch_master_msg_id());

        //2.保存t_prt

        petMapper.add(pet);

        //3.保存t_pet_detail
        PetDetail detail = pet.getDetail();
        if(detail!=null){
            detail.setPet_id(pet.getId());
            petDetailMapper.add(detail);
        }

    }


//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
